package com.yuexi.domain.bill;

import com.yuexi.domain.bill.bo.AddBillBO;
import com.yuexi.domain.bill.bo.AddBillDetailBO;
import com.yuexi.domain.bill.bo.BillBO;
import com.yuexi.domain.bill.bo.BillQueryBO;
import com.yuexi.domain.repository.BillDetailRepository;
import com.yuexi.domain.repository.BillRepository;
import com.yuexi.domain.util.IDType;
import com.zenx.domain.Entity;
import com.zenx.util.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author jie
 * @date 2023/4/6 -11:16
 */
@Entity
public class BillDomain {

    @Autowired
    private BillRepository repo;
    @Autowired
    private BillDetailRepository billDetailRepository;

    public List<BillBO> list(BillQueryBO bo) {
        return repo.list(bo);
    }

    /**
     * 生成外部订单号
     */
    public String generateOutTradeNo() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String dateString = dateFormat.format(new Date());
        String randomString = String.valueOf(new Random().nextInt(900000) + 100000);
        return dateString + randomString;
    }

    /**
     * 新增账单
     */
    public BillBO add(AddBillBO bo) {
        bo.setId(IDUtils.getSnowId(IDType.BILL));
        return repo.add(bo);
    }

    /**
     * 新增账单详情
     */
    public void addDetail(AddBillDetailBO bo) {
        bo.setId(IDUtils.getSnowId(IDType.BILL_DETAIL));
        billDetailRepository.add(bo);
    }
}
